import { reg, check } from "../../../api/User.js";
import "../../../Utils/lodash.js";

const oForms = document.forms[1];
const oBtns = oForms.querySelector(".btn");

oForms.uname.oninput = _.debounce(
  function () {
    const uname = oForms.uname.value;
    let self = this;
    const res = check(uname).then(function (res) {
      // console.log(res);
      let {
        data: { code, msg },
      } = res;

      self.nextElementSibling.innerHTML = msg.fontcolor(code ? "green" : "red");
    });
  },

  500
);

oBtns.onclick = async function () {
  const users = {
    uname: oForms.uname.value,
    upwd: oForms.upwd.value,
    uage: oForms.uage.value,
    uphone: oForms.uphone.value,
    realname: oForms.realname.value,
  };
  const res = await reg(users);
  const {
    data: { code, msg },
  } = res;
  if (code) {
    location = "../login/login.html";
  }
  alert(msg);
  //   console.log(code, msg);
  //   console.log(res);
  //   console.log(uname);

  return false;
};
